package com.xingongchang.baolebei.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.adapter.ProductListAdapter;
import com.xingongchang.baolebei.model.GoodsListModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.protocol.FILTER;
import com.xingongchang.baolebei.protocol.PAGINATED;

/**
 * @类名:CateogryFragment
 */
@SuppressLint("ValidFragment")
public class HomeCateogryFragment extends Fragment implements BusinessResponse,
		XListView.IXListViewListener {
	private XListView xlistView;
	String defaultTitle = "";
	int categoryid = 0;
	private GoodsListModel dataModel;
	private ProductListAdapter listAdapter;
	private boolean isSetAdapter = true;
	FILTER filter = new FILTER();
	public static final int IS_HOT = 0;
	public static final int PRICE_DESC_INT = 1;
	public static final int PRICE_ASC_INT = 2;
	public static final int TIME_DESC_INT = 3;
	public static final int TIME_ASC_INT = 4;
	public static final int SALE_DESC_INT = 5;
	public static final int SALE_ASC_INT = 6;
	private int flag = IS_HOT;
	private int flag_price = PRICE_ASC_INT; //默认是价格升序排序
	private int flag_time = TIME_DESC_INT; //默认是时间倒序排序
	private int flag_sale = SALE_DESC_INT; //默认是销量降序排序
	
	protected class TitleCellHolder
	{
		public ImageView 	arrowImageView; //箭头
		public TextView 	titleTextView; //文字
        public RelativeLayout tabRelative; //按钮relativelayout
	}
	
	TitleCellHolder tabOneCellHolder;
	TitleCellHolder tabTwoCellHolder;
	TitleCellHolder tabThreeCellHolder;
	TitleCellHolder tabFourCellHolder;
	View tab_view;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public static HomeCateogryFragment newInstance(String title, int id) {
		HomeCateogryFragment fragment = new HomeCateogryFragment(title, id);
		return fragment;
	}

	public HomeCateogryFragment(String defaultTitle, int id) {
		this.defaultTitle = defaultTitle;
		this.categoryid = id;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mView = inflater.inflate(R.layout.content_layout, null);
		tab_view = (View) mView.findViewById(R.id.tab_view);
		tab_view.setVisibility(View.VISIBLE);
		xlistView = (XListView) mView.findViewById(R.id.goods_listview);
		xlistView.setPullLoadEnable(true);
		xlistView.setRefreshTime();
		xlistView.setXListViewListener(this, 1);
		dataModel = new GoodsListModel(getActivity());
		filter.category_id = String.valueOf(categoryid);

		try {
			String filter_string = filter.toJson().toString();
			if (null != filter_string) {
				try {
					JSONObject filterJSONObject = new JSONObject(filter_string);
					filter = com.xingongchang.baolebei.protocol.FILTER
							.fromJson(filterJSONObject);
					filter.sort_by = dataModel.IS_HOT;

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		dataModel.addResponseListener(this);

	    tabOneCellHolder = new TitleCellHolder();
        tabTwoCellHolder = new TitleCellHolder();
        tabThreeCellHolder = new TitleCellHolder();
        tabFourCellHolder = new TitleCellHolder();

        tabOneCellHolder.titleTextView = (TextView)mView.findViewById(R.id.defult_text);
        tabOneCellHolder.tabRelative = (RelativeLayout)mView.findViewById(R.id.tab_defult);
        tabOneCellHolder.tabRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedTab(IS_HOT);
                tabOneCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_select));
                tabTwoCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabThreeCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabFourCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
            }
        });

        tabTwoCellHolder.titleTextView = (TextView)mView.findViewById(R.id.sales_text);
        tabTwoCellHolder.arrowImageView = (ImageView)mView.findViewById(R.id.order_sales);
        tabTwoCellHolder.tabRelative = (RelativeLayout)mView.findViewById(R.id.tab_sales);
        tabTwoCellHolder.tabRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabOneCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabTwoCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_select));
                tabThreeCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabFourCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                if(flag_sale == SALE_DESC_INT){
                	selectedTab(PRICE_DESC_INT);
                	flag_sale = SALE_ASC_INT;
                	tabTwoCellHolder.arrowImageView.setImageResource(R.drawable.order_down);
                	
                }else if(flag_sale == SALE_ASC_INT){
                	selectedTab(TIME_ASC_INT);
                	flag_sale = SALE_DESC_INT;
                	tabTwoCellHolder.arrowImageView.setImageResource(R.drawable.order_up);
                }
            }
        });

        tabThreeCellHolder.titleTextView = (TextView)mView.findViewById(R.id.price_text);
        tabThreeCellHolder.arrowImageView = (ImageView)mView.findViewById(R.id.order_price);
        tabThreeCellHolder.tabRelative  = (RelativeLayout)mView.findViewById(R.id.tab_price);
        tabThreeCellHolder.tabRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabOneCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabTwoCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabThreeCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_select));
                tabFourCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                if(flag_price == PRICE_DESC_INT){
                	selectedTab(PRICE_DESC_INT);
                	flag_price = PRICE_ASC_INT;
                	tabThreeCellHolder.arrowImageView.setImageResource(R.drawable.order_down);
                	
                }else if(flag_price == PRICE_ASC_INT){
                	selectedTab(PRICE_ASC_INT);
                	flag_price = PRICE_DESC_INT;
                	tabThreeCellHolder.arrowImageView.setImageResource(R.drawable.order_up);
                }
            }
        });
        
        tabFourCellHolder.titleTextView = (TextView)mView.findViewById(R.id.time_text);
        tabFourCellHolder.arrowImageView = (ImageView)mView.findViewById(R.id.order_time);
        tabFourCellHolder.tabRelative = (RelativeLayout)mView.findViewById(R.id.tab_time);
        tabFourCellHolder.tabRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedTab(TIME_ASC_INT);
                tabOneCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabTwoCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabThreeCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_custom));
                tabFourCellHolder.titleTextView.setTextColor(getResources().getColor(R.color.title_select));
                if(flag_time == TIME_DESC_INT){
                	selectedTab(TIME_DESC_INT);
                	flag_time = TIME_ASC_INT;
                	tabFourCellHolder.arrowImageView.setImageResource(R.drawable.order_down);
                	
                }else if(flag_time == TIME_ASC_INT){
                	selectedTab(TIME_ASC_INT);
                	flag_time = TIME_DESC_INT;
                	tabFourCellHolder.arrowImageView.setImageResource(R.drawable.order_up);
                }
            }
        });
        
        selectedTab(IS_HOT);
        flag = IS_HOT;
		return mView;
	}

	void selectedTab(int index) {
		isSetAdapter = true;

		if (index == IS_HOT) {

			filter.sort_by = dataModel.IS_HOT;
			dataModel.fetchPreSearch(filter);

		} else if (index == PRICE_DESC_INT) {

			filter.sort_by = dataModel.PRICE_DESC;
			dataModel.fetchPreSearch(filter);
			;
		} else if(index == PRICE_ASC_INT){

			filter.sort_by = dataModel.PRICE_ASC;
			dataModel.fetchPreSearch(filter);

		}else if(index == TIME_DESC_INT){

			filter.sort_by = dataModel.TIME_DESC;
			dataModel.fetchPreSearch(filter);

		}else if(index == TIME_ASC_INT){

			filter.sort_by = dataModel.TIME_ASC;
			dataModel.fetchPreSearch(filter);

		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}


	@Override
	public void onRefresh(int id) {
		// TODO Auto-generated method stub
		isSetAdapter = true;
		dataModel.fetchPreSearch(filter);
	}

	@Override
	public void onLoadMore(int id) {
		// TODO Auto-generated method stub
		isSetAdapter = false;
		dataModel.fetchPreSearchMore(filter);
	}

	public void setContent() {

		if (listAdapter == null) {

			if (dataModel.simplegoodsList.size() == 0) {

			} else {
				listAdapter = new ProductListAdapter(getActivity(),
						dataModel.simplegoodsList);
				xlistView.setAdapter(listAdapter);
			}

		} else {

			if (isSetAdapter == true) {
				listAdapter = new ProductListAdapter(getActivity(),
						dataModel.simplegoodsList);
				xlistView.setAdapter(listAdapter);
			} else {
				listAdapter.dataList = dataModel.simplegoodsList;
				listAdapter.notifyDataSetChanged();
			}
		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		// TODO Auto-generated method stub
		if (url.endsWith(ProtocolConst.SEARCH)) {
			xlistView.stopRefresh();
			xlistView.stopLoadMore();
			xlistView.setRefreshTime();

			setContent();
			PAGINATED paginated = PAGINATED.fromJson(jo
					.optJSONObject("paginated"));
			if (0 == paginated.more) {
				xlistView.setPullLoadEnable(false);
			} else {
				xlistView.setPullLoadEnable(true);
			}
		}
	}
}
